import 'package:flutter_wan_android/generated/json/base/json_convert_content.dart';

class HttpResult<T> {
  static const int HTTP_STATUS_OK = 0;

  static const String _JSON_MESSAGE = "errorMsg";
  static const String _JSON_CODE = "errorCode";
  static const String _JSON_DATA = "data";

  int _code;
  String _message;
  T _data;

  int get code => _code;

  String get message => _message;

  T get data => _data;

  HttpResult.parseData(Map<String, dynamic> map) {
    _code = int.parse(map[_JSON_CODE]?.toString());
    _message = map[_JSON_MESSAGE];

    /// 如果传递的为dynamic/String直接返回数据
    if (T.toString() == "dynamic" || T.toString() == "String") {
      _data = map[_JSON_DATA];
    } else {
      /// 其他简单类型进行json解析
      dynamic dynamicData = map[_JSON_DATA];
      if (dynamicData != null) {
        _data = JsonConvert.fromJsonAsT<T>(dynamicData);
      }
    }
  }
}
